<template>
  <div class="cluster recovery">
    <a-card style="width: 100%; min-height: 100%">
      <p slot="title" class="cardTitles">
        <span>考试列表</span>
      </p>
      <div class="formDiv">
        <a-form layout="inline">
          <a-form-item label="标题">
            <a-input v-model="name" type="text" style="width: 180px" />
          </a-form-item>
          <a-form-item label="站段">
            <a-select v-model="section" style="width: 180px">
              <a-select-option v-for="section in sectionData" :key="section">
                {{ section }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="search">搜索</a-button>
          </a-form-item>
          <a-form-item>
            <a-button @click="reset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>
      <div style="margin-top: 20px" class="table-content">
        <a-table
          bordered
          :columns="columns"
          :data-source="list"
          rowKey="id"
          :loading="loading"
          :pagination="pagination"
          @change="examChangePage"
        >
          <template slot-scope="text, record" slot="action">
            <a class="tools" @click="detail(record)" v-if="loginUserAuth!==3">编辑</a>
            <a class="tools" @click="deletetd(record)" v-if="loginUserAuth!==3">删除</a>
            <a class="tools" @click="examResult(record)">考试详情</a>
          </template>
        </a-table>
      </div>
    </a-card>
  </div>
</template>

<script>
import addModel from "components/configuration/management/addModel";
import { getExamTasksList,deleExamTasks} from "@/api/examination";
import { paging } from "assets/js/mixin";
import { getSectionArr } from "@/assets/js/utils";
export default {
  mixins: [paging],
  components: {
    addModel,
  },
  data() {
    return {
      loginUserAuth:JSON.parse(localStorage.getItem("userInfo")).auth,
      loading:false,
      name: "",
      section: "",
      createDate: "",
      endDate: "",
      sectionData: getSectionArr(),
      columns: [
        {
          title: "标题",
          dataIndex: "name",
        },
        {
          title: "站段",
          dataIndex: "section",
        },
        {
          title: "可考试次数",
          dataIndex: "tryTimes",
        },
        {
          title: "开始时间",
          dataIndex: "startTime",
        },
        {
          title: "结束时间",
          dataIndex: "endTime",
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
        },
        {
          title: "操作",
          dataIndex: "action",
          align: "center",
          scopedSlots: { customRender: "action" },
        },
      ],
      list: [],
      formLayout: {
        spanCol: { span: 6 },
        wrapperCol: { span: 18 },
      },
    };
  },
  mounted () {
    this.getExamTasksData();
    if(this.loginUserAuth === 3){
      this.section = JSON.parse(localStorage.getItem("userInfo")).section
    }
  },
  methods: {
    //搜索
    search() {
      this.pageNum = 1;
      this.pagination.current = 1;
      this.getExamTasksData();
    },
    //重置
    reset() {
      this.pageNum = 1;
      this.pagination.current = 1;
      this.section = "";
      this.name = "";
      this.getExamTasksData();
    },
    examChangePage(pagination){
      this.pageNum = pagination.current;
      this.pagination.current = pagination.current;
      this.pagination.pageSize = pagination.pageSize;
      this.getExamTasksData();
    },
    //获取考试列表
    getExamTasksData(){
      this.loading = true;
      var params = {
        page: this.pageNum,
        limit: this.pageSize,
        section: this.section,
        name:this.name
      };
      getExamTasksList(params)
        .then((res) => {
          this.list = res.data.examSimpleInfo;
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch((err) => {
          this.$message.destroy();
          this.$message.error(err);
        });
    },
    detail(recode) {
      this.$router.push({ name: "examinationCreat", query: { id: recode.id,type:1} });
    },
    examResult(recode){
      this.$router.push({ name: "examinationDetail", query: { id: recode.id} });
    },
    deletetd(recode) {
      var _this = this;
      this.$confirm({
        title: "确定要删除该条考试记录吗？",
        okText: "确定",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          deleExamTasks(recode.id)
            .then((res) => {
              if (res.code === 10002) {
                _this.$message.error(res.msg);
              } else if(res.code ===2){
                _this.$message.error(res.msg);
              } else {
                 _this.$message.success("删除成功");
                 _this.getExamTasksData();
              }
            })
            .catch((err) => {
              _this.$message.destroy();
              _this.$message.error(err);
            });
        },
      });
    },
  },
};
</script>

<style lang="stylus" scoped>
.formDiv {
  .lable {
    margin-right: 10px;
  }
}

.upload {
  float: right;
}

.recovery {
  height: 100%;
}

.table-content {
  height: calc(100% - 138px);
  overflow: auto;
}

.ant-card-body {
  height: 100%;
}
</style>